/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.framework.jwt.security.entity;

import cn.star.framework.core.IdEntity;
import cn.star.framework.jwt.types.TokenType;
import cn.star.framework.strategy.IdStrategyUuidValue;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Table;
import lombok.Data;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.GenericGenerator;

/**
 * Token持久化<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
@Data
@Entity
@Table(
    name = "sys_token",
    indexes = {@Index(columnList = "access_token_")})
@org.hibernate.annotations.Table(appliesTo = "sys_token", comment = "Jwt Token Storage")
public class Token implements IdEntity<String> {
  public Token() {}

  public Token(String accessToken, String clientIp, TokenType type) {
    this.accessToken = accessToken;
    this.clientIp = clientIp;
    this.type = type;
  }

  /** 主键 */
  @Id
  @Column(name = "id_", length = 32)
  @GeneratedValue(generator = IdStrategyUuidValue.STRATEGY)
  @GenericGenerator(name = IdStrategyUuidValue.STRATEGY, strategy = IdStrategyUuidValue.STRATEGY)
  @Comment(value = "主键(Uuid策略)")
  private String id;

  /** 访问Token */
  @Column(name = "access_token_", length = 2048, unique = true)
  @Comment(value = "访问Token")
  private String accessToken;

  /** 客户端Ip */
  @Column(name = "client_ip_")
  @Comment(value = "客户端Ip")
  private String clientIp;

  /** Token类型{@link TokenType} */
  @Column(name = "type_", length = 32)
  @Comment(value = "Token类型")
  @Enumerated(EnumType.STRING)
  private TokenType type;

  /**
   * 原始Token<br>
   * 只有刷新Token会有，记录刷新前的Token
   */
  @Column(name = "primitive_token_", length = 2048)
  @Comment(value = "原始Token")
  private String primitiveToken;
}
